package top.amot.forceview.drawitem

import android.content.Context
import android.graphics.Canvas
import android.graphics.Path
import android.graphics.PointF
import top.amot.forceview.layout.I_Layout
import top.amot.forceview.model.AbstractLink
import top.amot.forceview.model.AbstractNode
import top.amot.forceview.theme.I_Theme

abstract class AbstractDrawLink<T : AbstractLink<AbstractNode,AbstractNode>>(val context: Context,val drawManager:DrawManager) {

    abstract fun isType(link: AbstractLink<AbstractNode,AbstractNode>): Boolean

     fun draw(
        canvas: Canvas,
        theme: I_Theme,
        layout: I_Layout,
        link: AbstractLink<AbstractNode,AbstractNode>,
        isSelectNode: Boolean
    ){
         drawLink(canvas, theme, layout, link as T, isSelectNode)
     }

    abstract fun drawLink(
        canvas: Canvas,
        theme: I_Theme,
        layout: I_Layout,
        link: T,
        isSelectNode: Boolean
    )

    abstract fun calcLinkPath(link: T): Path

    fun getSourceStartPoint(node:AbstractNode,position: LinkPointPosition=LinkPointPosition.RIGHT): PointF {
        return calcLinkPoint(node,position)
    }

    fun getTargetStartPoint(node: AbstractNode,position: LinkPointPosition=LinkPointPosition.LEFT): PointF {
        return calcLinkPoint(node,position)
    }

    private fun calcLinkPoint(node:AbstractNode,position: LinkPointPosition): PointF {
         drawManager.findDrawItem(node)?.let {
             return it.calcLinkPoint(node,position)
         }
        return PointF(node.x, node.y)
    }

    abstract fun calcLinkText(link:T):PointF
}